package com.noble.glamour.module.logic.socket;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.noble.glamour.module.logic.socket.stream.NetStream;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * socket消息发送结构体
 */
public class NetData implements Parcelable {
    private int msgType = -1;       // 消息类型：如 MSG_ID_HEART_BEAT3
    private long messageId = -1;    // 消息id，对应消息体中的"Id"字段，不存在消息id则为-1
    private long fromId = -1;       // 消息发送者id，不存在fid则为-1
    private long tuid = -1;         // 消息接受者id，不存在则为-1
    private String content = "";    // content字段 json体

    private String msgBody = "";    // 最终拼接供传输的JSON消息体

    /**
     * 空数据包
     */
    public NetData() {
    }

    /**
     * 接收到的消息数据体
     */
    public NetData(String msgBody) {
        this.msgBody = msgBody;
    }

    /**
     * Socket传输消息体
     */
    public byte[] getBytes() {
        return NetStream.getBytes(msgBody);
    }

    /**
     * 消息id:  解析返回消息体，不存在返回-1
     */
    public Long getMessageId() {
        if (messageId == -1 && !TextUtils.isEmpty(msgBody)) {
            try {
                JSONObject contentJ = new JSONObject(msgBody);
                messageId = contentJ.optLong("Id", -1);
            } catch (JSONException e) {
            }
        }
        return messageId;
    }

    /**
     * 重置或添加消息Id，仅对已加密过得数据体使用
     * 如：已使用以下构造方法后使用
     * public NetData(long uid, int msgType, String content)
     */
    public void setMessageId(long msgId) {
        if (msgId == -1 || TextUtils.isEmpty(msgBody)) return;
        try {
            JSONObject contentJ = new JSONObject(msgBody);
            contentJ.put("Id", msgId);
            content = contentJ.toString();
            this.messageId = msgId;
        } catch (JSONException e) {
        }
    }

    /**
     * 发送者id:  不存在消息体或不存在发送者则返回-1
     * <p>
     * 10000: 表示系统发送
     */
    public Long getFromId() {
        if (fromId == -1 && !TextUtils.isEmpty(msgBody)) {
            try {
                JSONObject contentJ = new JSONObject(msgBody);
                fromId = contentJ.optLong("UidFrom", -1);
            } catch (JSONException e) {
            }
        }
        return fromId;
    }

    public void setFromId(long fromId) {
        this.fromId = fromId;
    }

    /**
     * 接收者id:  不存在消息体或不存在接收者则返回-1
     */
    public long getTuid() {
        if (tuid == -1 && !TextUtils.isEmpty(msgBody)) {
            try {
                JSONObject contentJ = new JSONObject(msgBody);
                tuid = contentJ.optLong("UidTo", -1);
            } catch (JSONException e) {
            }
        }
        return tuid;
    }

    public void setTuid(long tuid) {
        this.tuid = tuid;
    }

    /**
     * 消息类型
     */
    public int getMsgType() {
        if (msgType == -1 && !TextUtils.isEmpty(msgBody)) {
            try {
                JSONObject contentJ = new JSONObject(msgBody);
                msgType = contentJ.optInt("TypeMsg", -1);
            } catch (JSONException e) {
            }
        }
        return msgType;
    }

    public void setMsgType(int msgType) {
        this.msgType = msgType;
    }

    /**
     * 消息内容
     */
    public String getContent() {
        if (TextUtils.isEmpty(content) && !TextUtils.isEmpty(msgBody)) {
            try {
                JSONObject contentJ = new JSONObject(msgBody);
                content = contentJ.optString("Content", "");
            } catch (JSONException e) {
            }
        }
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 服务器时间戳
     */
    public long getServerTime() {
        if (!TextUtils.isEmpty(msgBody)) {
            try {
                JSONObject bodyObject = new JSONObject(msgBody);
                return bodyObject.optLong("TM");
            } catch (JSONException e) {
            }
        }
        return 0;
    }

    /**
     * 整个消息体
     */
    public String getMsgBody() {
        return msgBody;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.content);
        dest.writeInt(this.msgType);
        dest.writeLong(this.messageId);
        dest.writeLong(this.fromId);
        dest.writeLong(this.tuid);
        dest.writeString(this.msgBody);
    }

    protected NetData(Parcel in) {
        this.content = in.readString();
        this.msgType = in.readInt();
        this.messageId = in.readLong();
        this.fromId = in.readLong();
        this.tuid = in.readLong();
        this.msgBody = in.readString();
    }

    public static final Creator<NetData> CREATOR = new Creator<NetData>() {
        @Override
        public NetData createFromParcel(Parcel source) {
            return new NetData(source);
        }

        @Override
        public NetData[] newArray(int size) {
            return new NetData[size];
        }
    };

    @Override
    public String toString() {
        return "msgBody= " + msgBody;
    }
}
